package com.googlecode.stemwijzer.client.event;

import com.google.gwt.event.shared.GwtEvent;
import com.googlecode.stemwijzer.client.event.handler.ExceptionHandler;

public class ExceptionEvent extends GwtEvent<ExceptionHandler> {

	public static final Type<ExceptionHandler> TYPE = new Type<ExceptionHandler>();

	private Throwable throwable;

	private String message;

	public ExceptionEvent(final String message) {
		this.message = message;
	}

	public ExceptionEvent(final String message, final Throwable t) {
		this.message = message;
		this.throwable = t;
	}

	@Override
	public Type<ExceptionHandler> getAssociatedType() {
		return TYPE;
	}

	@Override
	protected void dispatch(ExceptionHandler handler) {
		handler.onException(this);
	}

	public Throwable getThrowable() {
		return throwable;
	}

	public String getMessage() {
		String result = message;
		if (throwable != null) {
			result += " - " + throwable.getMessage();
		}
		return result;
	}
}
